package com.hyacinth.config;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * 数据库类型
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public enum DBType {
    MYSQL(1, "com.mysql.jdbc.Driver", "jdbc:mysql://{host}:{port}/{dbname}?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useInformationSchema=true"),
    SQLSERVER(2, "com.microsoft.jdbc.sqlserver.SQLServerDriver", "jdbc:sqlserver://{host}:{port};databaseName={dbname}"),
    ORACLE(3, "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@//{host}:{port}/{dbname}"),
    DAMENG(4, "dm.jdbc.driver.DmDriver","jdbc:dm://{host}:{port}/{dbname}"),
    DB2(5, "com.ibm.db2.jdbc.app.DB2Driver", "jdbc:db2://{host}:{port}/{dbname}");


    private int value = 0;
    private String driverName;
    private String urlFormat;

    DBType(int value, String driverName, String urlFormat) {
        this.value = value;
        this.driverName = driverName;
        this.urlFormat = urlFormat;
    }

    public static DBType valueOf(int value) {
        switch(value) {
            case 1:
                return MYSQL;
            case 2:
                return SQLSERVER;
            case 3:
                return ORACLE;
            case 4:
                return DAMENG;
            case 5:
                return DB2;
            default:
                return null;
        }
    }

    /**
     * 根据driverName获取数据库类型
     * @param driverName driverName
     * @return 数据库类型
     */
    public static DBType getByDriverName(String driverName) {
        Stream<DBType> dbTypeStream = Arrays.stream(DBType.values());
        Optional<DBType> first = dbTypeStream.filter(type -> type.driverName.equalsIgnoreCase(driverName)).findFirst();
        return first.orElse(null);
    }

    public int value() {
        return this.value;
    }

    public String driverName() {
        return this.driverName;
    }

    public String urlFormat() {
        return this.urlFormat;
    }
}
